// pages/login/index.js
const db = wx.cloud.database()
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },
    //点击获取用户信息
    handleGetUserInfo() {
        wx.showLoading({
            title: '登陆中...',
        })
        wx.getUserProfile({
            desc: '完善用户资料',
            success(res) {
                wx.hideLoading()
                const userInfo = res.userInfo
                console.log(userInfo)
                db.collection("userInfo")
                    .where({ _openid: '{openid}' })
                    .get()
                    .then(res => {
                        console.log(res)
                        if (res.data.length == 0) { //未选择过地址 - add
                            //添加入数据表
                            db.collection('userInfo')
                                .add({
                                    data: {
                                        isLogin: true,
                                        _openid: '{openid}',
                                        nickName: userInfo.nickName,
                                        avatarUrl: userInfo.avatarUrl,
                                        address: [],
                                        campus: ''
                                    }
                                }).then(res => {
                                    wx.showToast({
                                        icon: 'none',
                                        title: '登录成功~',
                                    })
                                    setTimeout(() => {
                                        wx.navigateBack({
                                            delta: 1,
                                        })
                                    }, 800);
                                }).catch(res => {
                                    console.log(res)
                                    wx.showToast({
                                        title: '您已经登陆过啦',
                                    })
                                })
                        } else { //选择过地址 - update
                            //添加入数据表
                            wx.cloud.callFunction({
                                name: 'userInfo',
                                data: {
                                    useType: '登陆',
                                    userData: {
                                        nickName: userInfo.nickName,
                                        avatarUrl: userInfo.avatarUrl,
                                        address: [],
                                        isLogin: true,
                                    }
                                }
                            }).then(res => {
                                console.log(res)
                                wx.showToast({
                                    icon: 'none',
                                    title: '登录成功~',
                                })
                                setTimeout(() => {
                                    wx.navigateBack({
                                        delta: 1,
                                    })
                                }, 800);
                            }).catch(res => {
                                console.log(res)
                                wx.showToast({
                                    title: '您已经登陆过啦',
                                })
                            })
                        }
                    })
                    .catch(res => {
                        console.log(res)
                    })



            },
            fail(res) {
                wx.hideLoading()
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1,
                    })
                }, 800);
            }
        })
    },
})